package com.ymatou.iapi.optrade.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFeeTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFreightTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.ApplyRefundV2Req;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.ApplyRefundV2Call;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceAnliOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceAuthenticateCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceEngageOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingProductVo;
import com.ymttest.common.order.userquery.util.Utils;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.RefundBillPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 申请退货退款接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ApplyRefundV2 {

	private static ApplyRefundV2Req applyRefundReq;
	private static ApplyRefundV2Call applySalesRefundCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long userId = 20238699;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("申请退货退款接口-买家");
		
		String accountid1;
		try {
			accountid1 = AccountCallServiceV2.getAccountId(20238699);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					9000000), new BigDecimal(0));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {

		applyRefundReq = new ApplyRefundV2Req();
		applySalesRefundCall = new ApplyRefundV2Call();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(applySalesRefundCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	/**
	 * 1个买手,2个商品，第二件商品包邮
	 * @return
	 */
	public static TradingServiceResp placeOrder3() {

		Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
		String sellerName = String.valueOf(EnvSetup.getData("autotest_sellerName1"));
		
		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		TradingProductVo productVo1 = TradingProductVo.createPro(sellerId,
				sellerName, 10);
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				product.getProductId(), product.getCatalogId());
		OrderItemVo orderItemVo1 = TradingCallService.createOrderItemVo(
				productVo1.getProductId(), productVo1.getCatalogId());
		
		// 包邮
		orderItemVo1.setFreight(BigDecimal.ZERO);
		
		orderVo.setOrderItems(Arrays.asList(orderItemVo, orderItemVo1));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));	

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}

	//商品申请退款的上限值为：当个商品最大可退款金额+订单总运费

	// 计算订单总价格 商品价格 X 数量+运费
	/*	
	 * 
	 * BigDecimal refoundAmout = orderDetailPo.getProductPrice()
				.multiply(new BigDecimal(productNum))
				.subtract(orderDetailPo.getYmtCouponAmount())
				.subtract(orderDetailPo.getSellerCouponAmount());
				
		//单件商品可退金额
		BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
				productNum),2,BigDecimal.ROUND_DOWN);
				
				*/

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_001() {
		Logger.start(true, "退商品及商品运费,申请退货退款成功");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(true, BigDecimal.ZERO,
							userId, LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			long orderId = order.getOrderId();

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 付款			
			TradingCallService.payOrder(userId, amount, tradingId);

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 非贝海发货
			TradingCallService.delivery(sellerId, orderId, false);

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

//			applyRefundReq.setApplyRefundFreight(BigDecimal.ONE);
			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_002() {
		Logger.start(false, "申请退货退款,订单号不存在");
		try {

			applyRefundReq.setOrderId(11111);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq.setCatalogId("sssss");

			applyRefundReq.setRefundBillNo(String.format("TK%s", 12323));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderNotExists",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单不存在", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_004() {
		Logger.start(true, "发货后,部分退款");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(true, BigDecimal.ZERO,
							userId, LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			long orderId = order.getOrderId();

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();
			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 付款			
			TradingCallService.payOrder(userId, amount, tradingId);

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_005() {
		Logger.start(true, "同一退款单,重复申请退款");
		try {
			TradingServiceResp placeOrderResp = TradingCallService
					.orderApplyRefund();

			Order order = placeOrderResp.getOrderList().get(0);

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			long orderId = order.getOrderId();

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Thread.sleep(2000);

			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("申请退款金额不能大于实际可退款金额",
							applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_007() {
		Logger.start(true, "商品申请退款金额大于(商品可退金额+订单总运费)");

		try {
			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			//第一个订单下的商品列表
			BigDecimal orderFreight = order.getOrderFreight();

			applyRefundReq.setOrderId(orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			String catalogId = orderDetailPo.getCatalogId();

			int productNum = orderDetailPo.getProductCount();

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderDetailPo.getProductPrice()
					.multiply(new BigDecimal(productNum)).add(orderFreight)
					.add(BigDecimal.ONE);

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(refoundAmout);
			applyRefundReq.setCatalogId(catalogId);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo res = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("申请退款金额不能大于实际可退款金额",
							applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

			Logger.verifyEquals(null, res, "退款业务表（refund_bill）记录数为0");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_008() {
		Logger.start(true, "订单商品不是包邮,商品申请退款,只退商品,不退运费");
		try {
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false, BigDecimal.ZERO,
							userId, LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			long orderId = order.getOrderId();

			//第一个订单下的商品列表			
			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 付款
			TradingCallService.payOrder(userId, amount, tradingId);
			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			applyRefundReq.setOrderId(orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			String catalogId = orderDetailPo.getCatalogId();

			int productNum = orderDetailPo.getProductCount();

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderDetailPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderDetailPo.getYmtCouponAmount())
					.subtract(orderDetailPo.getSellerCouponAmount());

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(productNum);
			applyRefundReq.setApplyRefundAmount(refoundAmout);
			applyRefundReq.setCatalogId(catalogId);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_009() {
		Logger.start(true, "订单商品不是包邮,商品申请退款,商品部分退款");
		try {
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false, BigDecimal.ZERO,
							userId, LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			long orderId = order.getOrderId();

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			//第一个订单下的商品列表			
			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 付款
			TradingCallService.payOrder(userId, amount, tradingId);
			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ONE);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_010() {
		Logger.start(true, "订单商品不是包邮,商品申请退款,商品全退,退部分商品运费");
		try {
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false, BigDecimal.ZERO,
							userId, LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			long orderId = order.getOrderId();

			//第一个订单下的商品列表
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 付款
			TradingCallService.payOrder(userId, amount, tradingId);
			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(orderInfoPo.getProductCount()))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(orderInfoPo.getProductCount());
			applyRefundReq.setApplyRefundAmount(refoundAmout);
			applyRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();
			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_012() {
		Logger.start(true, "订单里有包邮非包邮混合商品,包邮商品全退+退部分运费");
		try {
			TradingServiceResp placeOrderResp = placeOrder3();

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();
			long orderId = order.getOrderId();
			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 付款
			TradingCallService.payOrder(userId, amount, tradingId);
			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			applyRefundReq.setOrderId(orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(1);

			String catalogId = orderDetailPo.getCatalogId();

			int productNum = orderDetailPo.getProductCount();

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderDetailPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderDetailPo.getYmtCouponAmount())
					.subtract(orderDetailPo.getSellerCouponAmount());

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ONE);
			applyRefundReq.setApplyRefundCount(productNum);
			applyRefundReq.setApplyRefundAmount(refoundAmout
					.add(BigDecimal.ONE));
			applyRefundReq.setCatalogId(catalogId);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			// 数据库验证
			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			Logger.verifyEquals(null, actualRefundBillPo,
					"退款业务表（refund_bill）记录数为0");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_017() {
		Logger.start(true, "定金订单全款支付后,确认收货前全部申请退货退款成功");
		try {
			
			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrder = TradingPlaceEngageOrderCallService
					.placeEngageOrder2Order(false);

			long userId = placeOrder.getUserId();
			Order order = placeOrder.getOrderList().get(0);
			long sellerId = order.getSellerId();
			
			//定金定单二次确认
			TradingServiceResp secondConfirmPlaceOrder = TradingPlaceEngageOrderCallService.placeFullPaidOrderWithAllPromotion(placeOrder);
			Order secondConfirmOrder = secondConfirmPlaceOrder.getOrderList().get(0);
			
			// 付全款
			TradingCallService.payOrder(userId, secondConfirmPlaceOrder.getPayableAmount(),
					secondConfirmPlaceOrder.getTradingId(), 1);
			TradingSyncService.getSyncResult(secondConfirmOrder.getOrderId());
			
			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, secondConfirmOrder.getOrderId());
			TradingSyncService.getSyncResult(secondConfirmOrder.getOrderId());
			
			Logger.comment("前置数据准备结束");

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();
			applyRefundReq.setOrderId(secondConfirmOrder.getOrderId());

			applyRefundReq.setApplyRefundFreight(secondConfirmOrder.getOrderFreight());
			applyRefundReq.setApplyRefundCount(orderDetailPoList.get(0).getProductCount());
			applyRefundReq.setApplyRefundAmount(secondConfirmOrder.getOrderPayableAmount());
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", secondConfirmOrder.getOrderId()));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(secondConfirmOrder.getOrderId());
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_018() {
		Logger.start(true, "定金订单全款支付后,确认收货前部分申请退货退款成功");
		try {

			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrder = TradingPlaceEngageOrderCallService
					.placeEngageOrder2Order(false);

			long userId = placeOrder.getUserId();
			Order order = placeOrder.getOrderList().get(0);
			long sellerId = order.getSellerId();
			
			//定金定单二次确认
			TradingServiceResp secondConfirmPlaceOrder = TradingPlaceEngageOrderCallService.placeFullPaidOrderWithAllPromotion(placeOrder);
			Order secondConfirmOrder = secondConfirmPlaceOrder.getOrderList().get(0);
			
			// 付全款
			TradingCallService.payOrder(userId, secondConfirmPlaceOrder.getPayableAmount(),
					secondConfirmPlaceOrder.getTradingId(), 1);
			TradingSyncService.getSyncResult(secondConfirmOrder.getOrderId());
			
			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, secondConfirmOrder.getOrderId());
			TradingSyncService.getSyncResult(secondConfirmOrder.getOrderId());
			
			Logger.comment("前置数据准备结束");

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();
			applyRefundReq.setOrderId(secondConfirmOrder.getOrderId());

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", secondConfirmOrder.getOrderId()));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(secondConfirmOrder.getOrderId());
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_019() {
		Logger.start(true, "购买安利会会员订单，申请退货退款失败");
		try {

			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrderResp = TradingPlaceAnliOrderCallService
					.placeMultiProAnlihuiOrder(BigDecimal.TEN, false, 2);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 用余额支付金额
			BigDecimal useAccountAmount = new BigDecimal(47);
			// 支付订单 余额+支付宝组合支付
			// 13 为 app国际支付宝，15 微信
			String payType = "13";

			Long thirdTradingId = TradingCallService.payOrder(
					placeOrderResp.getUserId(), useAccountAmount,
					placeOrderResp.getTradingId(), payType);

			// 支付网关支付回调交易
			PayGateWayCallService.notifyCall(payType,
					String.valueOf(thirdTradingId),
					totalPrice.subtract(useAccountAmount).toString(),
					userId.intValue());

			// 等待支付网关异步处理
			Thread.sleep(5000);
			
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();
			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ERR_STATUS_NOTMATCH",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("已购买的安利会员订单不能发起退货退款", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_020() {
		Logger.start(true, "下单使用返点金额，第三方+余额支付全款后。确认收货前,申请全额退货退款");
		try {
			
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceAnliOrderCallService
					.placeMultiProAnlihuiOrder(BigDecimal.TEN, false, 0);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 用余额支付金额
			BigDecimal useAccountAmount = new BigDecimal(47);
			// 支付订单 余额+支付宝组合支付
			// 13 为 app国际支付宝，15 微信
			String payType = "13";

			Long thirdTradingId = TradingCallService.payOrder(
					placeOrderResp.getUserId(), useAccountAmount,
					placeOrderResp.getTradingId(), payType);

			// 支付网关支付回调交易
			PayGateWayCallService.notifyCall(payType,
					String.valueOf(thirdTradingId),
					totalPrice.subtract(useAccountAmount).toString(),
					userId.intValue());

			// 等待支付网关异步处理
			Thread.sleep(5000);

			// 接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			//第一个订单下的商品列表
			String catalogId = order
					.getOrderDetailPoList().get(0).getCatalogId();
			
			OrderDetailPo orderDetailPo = new OrderShardingWapper()
					.selectOrderDetailbyOrderIdAndCatalogId(orderId, catalogId);
			
			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(orderDetailPo.getProductCount());
			applyRefundReq.setApplyRefundAmount(TradingCallService.getOrderDetailPaidAmountExcludeFreight(orderDetailPo));
			applyRefundReq
					.setCatalogId(catalogId);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(order.getSellerId());
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);
			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_021() {
		Logger.start(true, "下单使用返点金额，第三方+余额支付全款后。确认收货后, 申请部分退货退款");
		try {
			
			TradingServiceResp placeOrderResp = TradingPlaceAnliOrderCallService
					.placeMultiProAnlihuiOrder(BigDecimal.TEN, false, 0);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 用余额支付金额
			BigDecimal useAccountAmount = new BigDecimal(47);
			// 支付订单 余额+支付宝组合支付
			// 13 为 app国际支付宝，15 微信
			String payType = "13";

			Long thirdTradingId = TradingCallService.payOrder(
					placeOrderResp.getUserId(), useAccountAmount,
					placeOrderResp.getTradingId(), payType);

			// 支付网关支付回调交易
			PayGateWayCallService.notifyCall(payType,
					String.valueOf(thirdTradingId),
					totalPrice.subtract(useAccountAmount).toString(),
					userId.intValue());

			// 等待支付网关异步处理
			Thread.sleep(2000);

			// 接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(order.getSellerId(), orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(order.getSellerId(), orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			//第一个订单下的商品列表
			String catalogId = order
					.getOrderDetailPoList().get(0).getCatalogId();
			
			OrderDetailPo orderDetailPo = new OrderShardingWapper()
					.selectOrderDetailbyOrderIdAndCatalogId(orderId, catalogId);
			
			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(orderDetailPo.getProductCount());
			applyRefundReq.setApplyRefundAmount(TradingCallService.getOrderDetailPaidAmountExcludeFreight(orderDetailPo));
			applyRefundReq
					.setCatalogId(catalogId);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(order.getSellerId());
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);
			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_022() {
		Logger.start(true, "下单使用返点金额，第三方+余额支付全款后,确认收货后部分申请退货退款成功");
		try {

			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrder = TradingPlaceEngageOrderCallService
					.placeEngageOrder2Order(false);

			long userId = placeOrder.getUserId();
			Order order = placeOrder.getOrderList().get(0);
			long sellerId = order.getSellerId();
			
			//定金定单二次确认
			TradingServiceResp secondConfirmPlaceOrder = TradingPlaceEngageOrderCallService.placeFullPaidOrderWithAllPromotion(placeOrder);
			Order secondConfirmOrder = secondConfirmPlaceOrder.getOrderList().get(0);
			
			// 付全款
			TradingCallService.payOrder(userId, secondConfirmPlaceOrder.getPayableAmount(),
					secondConfirmPlaceOrder.getTradingId(), 1);
			TradingSyncService.getSyncResult(secondConfirmOrder.getOrderId());
			
			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, secondConfirmOrder.getOrderId());
			TradingSyncService.getSyncResult(secondConfirmOrder.getOrderId());
			
			Logger.comment("前置数据准备结束");

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();
			applyRefundReq.setOrderId(secondConfirmOrder.getOrderId());

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", secondConfirmOrder.getOrderId()));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(secondConfirmOrder.getOrderId());
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_023() {
		Logger.start(true, "购买海淘达人会员订单，申请退货退款失败");
		try {

			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrderResp = TradingPlaceAnliOrderCallService
					.placeMultiProAnlihuiOrder(BigDecimal.TEN, false, 3);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 用余额支付金额
			BigDecimal useAccountAmount = new BigDecimal(47);
			// 支付订单 余额+支付宝组合支付
			// 13 为 app国际支付宝，15 微信
			String payType = "13";

			Long thirdTradingId = TradingCallService.payOrder(
					placeOrderResp.getUserId(), useAccountAmount,
					placeOrderResp.getTradingId(), payType);

			// 支付网关支付回调交易
			PayGateWayCallService.notifyCall(payType,
					String.valueOf(thirdTradingId),
					totalPrice.subtract(useAccountAmount).toString(),
					userId.intValue());

			// 等待支付网关异步处理
			Thread.sleep(5000);
			
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();
			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ERR_STATUS_NOTMATCH",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("已购买的安利会员订单不能发起退货退款", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_025() {
		Logger.start(true, "订单商品满减包邮,申请单个商品退款+运费，申请退货退款失败");
		try {

			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithCoupon();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			// 支付订单
			TradingCallService.payOrder(userId, payAmount, tradingId);
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Thread.sleep(5000);

			Order order = placeOrderResp.getOrderList().get(0);

			// 第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			int productNum = orderInfoPo.getProductCount();

			// 计算订单总价格 商品价格 X 数量
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount())
					.add(orderInfoPo.getFreight());

			Logger.comment("前置数据准备结束");

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(orderInfoPo.getFreight());
			applyRefundReq.setApplyRefundCount(productNum);
			applyRefundReq.setApplyRefundAmount(refoundAmout);
			applyRefundReq
					.setCatalogId(orderInfoPo.getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("申请退款金额不能大于实际可退款金额", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_026() {
		Logger.start(true, "退商品及商品运费,申请退货退款成功");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(true, BigDecimal.ZERO,
							userId, LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			long orderId = order.getOrderId();

			//第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 付款			
			TradingCallService.payOrder(userId, amount, tradingId);

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPoList.get(0).getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_027() {
		Logger.start(true, "买家承担鉴定费和鉴定物流费, 接单后，部分退货退款，申请退款金额小于商品可退鉴别费金额，申请退货退款失败");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(AuthenticateFreightTypeEnum.BUYER,
							BigDecimal.ONE, AuthenticateFeeTypeEnum.BUYER,
							BigDecimal.TEN);
			Long userId = placeOrder.getUserId();
			
			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);
			
			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(BigDecimal.ONE);
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单%s申请退款鉴别费金额错误！", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_028() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 接单后，全部退货退款，申请退款金额=商品可退鉴别费金额+鉴别运费+商品可退金额+运费，申请退货退款成功");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Integer refundProduct = orderDetailPo.getProductCount();
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight())
					.add(order.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight());
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct)
					.add(order.getUserAuthenticateFreightFee()));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);
			expectedRefundBillPo.setRefundType(applyRefundReq.getRefundType());
			expectedRefundBillPo.setUserAuthenticateFee(applyRefundReq
					.getApplyRefundAuthenticateFee());

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_029() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 确认收货后，全部退货退款，申请退款金额=商品可退鉴别费金额+鉴别运费+商品可退金额+运费，申请退货退款成功");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			Thread.sleep(3000);
			
			// 发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, "Y013", 3, true);
			
			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Integer refundProduct = orderDetailPo.getProductCount();
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight())
					.add(order.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight());
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct).add(
						order.getUserAuthenticateFreightFee()));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);
			expectedRefundBillPo.setRefundType(applyRefundReq.getRefundType());
			expectedRefundBillPo.setUserAuthenticateFee(applyRefundReq
					.getApplyRefundAuthenticateFee());

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_030() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 确认收货后，全部退货退款，申请可退鉴别费>商品可退鉴别费金额，申请失败");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			
			// 发货
			TradingCallService.delivery(sellerId, orderId, false);
			
			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Integer refundProduct = orderDetailPo.getProductCount();
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight())
					.add(order.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight());
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct).add(
						order.getUserAuthenticateFreightFee()).add(BigDecimal.ONE));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单%s申请退款鉴别费金额错误！", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_031() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 确认收货后，部分退货退款，申请可退鉴别费>商品可退鉴别费金额，申请失败");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			
			// 发货
			TradingCallService.delivery(sellerId, orderId, false);
			
			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Integer refundProduct = 1;
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight())
					.add(order.getUserAuthenticateFee(refundProduct));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight());
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct).add(BigDecimal.ONE));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单%s申请退款鉴别费金额错误！", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_032() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 接单后，部分退货退款，申请可退鉴别费=商品可退鉴别费金额+鉴别运费，申请失败");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			
			// 发货
			TradingCallService.delivery(sellerId, orderId, false);
			
			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Integer refundProduct = 1;
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight())
					.add(order.getUserAuthenticateFee(refundProduct));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight());
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单%s申请退款鉴别费金额错误！", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_033() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 接单后，买家先退一件商品，剩下商品全退，申请金额=最大可退金额+商品可退鉴定费+鉴定运费+运费，申请成功");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 先退一个商品
			BigDecimal refundedFreight = BigDecimal.ONE;
			Integer refundedProductCount = 1;

			RefundService.buildSingleApplyRefundReq(orderId, orderDetailPo,
					refundedFreight, refundedProductCount);

			Integer refundProduct = orderDetailPo.getProductCount()
					- refundedProductCount;
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight().subtract(refundedFreight))
					.add(order.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight().subtract(refundedFreight));
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);
			expectedRefundBillPo.setRefundType(applyRefundReq.getRefundType());
			expectedRefundBillPo.setUserAuthenticateFee(applyRefundReq
					.getApplyRefundAuthenticateFee());

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_034() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 接单后，买家先退一件商品，剩下商品全退，申请金额>最大可退金额+商品可退鉴定费+鉴定运费+运费，申请失败");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 先退一个商品
			BigDecimal refundedFreight = BigDecimal.ONE;
			Integer refundedProductCount = 1;

			RefundService.buildSingleApplyRefundReq(orderId, orderDetailPo,
					refundedFreight, refundedProductCount);

			Integer refundProduct = orderDetailPo.getProductCount()
					- refundedProductCount;
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight().subtract(refundedFreight))
					.add(order.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()))
					.add(BigDecimal.ONE);

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight().subtract(refundedFreight));
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("申请退款金额不能大于实际可退款金额", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_035() {
		Logger.start(true, "先退一件商品，第二次退商品时，退款的商品数量大于总商品数量，申请退货退款失败");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(true, BigDecimal.ZERO,
							userId, LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			long orderId = order.getOrderId();

			//第一个商品
			OrderDetailPo orderDetailPo = order
					.getOrderDetailPoList().get(0);

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 付款			
			TradingCallService.payOrder(userId, amount, tradingId);

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 非贝海发货
			TradingCallService.delivery(sellerId, orderId, false);
			
			// 先退一个商品
			BigDecimal refundedFreight = BigDecimal.ONE;
			Integer refundedProductCount = 1;
			
			RefundService.buildSingleApplyRefundReq(orderId, orderDetailPo,
					refundedFreight, refundedProductCount);

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(orderDetailPo.getProductCount());
			applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
			applyRefundReq
					.setCatalogId(orderDetailPo.getCatalogId());

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("申请退商品数量不能大于实际商品数量", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_036() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 接单后，全部退货退款，申请退款金额=商品可退鉴别费金额+商品可退金额+运费，申请失败");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Integer refundProduct = orderDetailPo.getProductCount();
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight())
					.add(order.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));

			applyRefundReq.setOrderId(orderId);

			// applyRefundReq.setApplyRefundFreight(order.getOrderFreight());
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单%s申请退款鉴别费金额错误！", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyRefundV2_037() {
		Logger.start(true, "买家承担鉴定费和鉴定物流费, 接单后，部分退货退款，申请退款金额=商品可退鉴别费金额，申请失败");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(AuthenticateFreightTypeEnum.BUYER,
							BigDecimal.ONE, AuthenticateFeeTypeEnum.BUYER,
							BigDecimal.TEN);
			Long userId = placeOrder.getUserId();
			
			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);
			
			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();
			
			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(1);

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
			applyRefundReq.setApplyRefundCount(1);
			applyRefundReq.setApplyRefundAmount(userAuthenticateFee);
			applyRefundReq
					.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(userAuthenticateFee);
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单%s申请退款金额不能小于商品可退鉴别费金额", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_038() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 接单后，买家先退一件商品，剩下商品全退，申请金额=最大可退金额+商品可退鉴定费+鉴定运费+运费，申请成功");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 先退一个商品
			BigDecimal refundedFreight = BigDecimal.ONE;
			Integer refundedProductCount = 1;

			RefundService.buildSingleApplyRefundReq(orderId, orderDetailPo,
					refundedFreight, refundedProductCount);

			Integer refundProduct = orderDetailPo.getProductCount()
					- refundedProductCount;
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight().subtract(refundedFreight))
					.add(order.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight().subtract(refundedFreight));
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);
			expectedRefundBillPo.setRefundType(applyRefundReq.getRefundType());
			expectedRefundBillPo.setUserAuthenticateFee(applyRefundReq
					.getApplyRefundAuthenticateFee());

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_039() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 鉴定中心发货，确认收货后，全部退货退款，申请退款金额=商品可退鉴别费金额+鉴别运费+商品可退金额+运费，申请退货退款失败");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			Thread.sleep(3000);
			
			// 发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, "Y013", 3, true);
			
			// 鉴定中心发货
			TradingCallService.authenticateDelivery(sellerId, orderId);
			Thread.sleep(3000);
			
			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Integer refundProduct = orderDetailPo.getProductCount();
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight())
					.add(order.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight());
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct).add(
						order.getUserAuthenticateFreightFee()));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("false",
					applySalesRefundCall.getString("success"), "验证success");

			Logger.verifyEquals("ErrOrderAmountNotMatch",
					applySalesRefundCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单%s申请退款金额不能大于商品最大可退款金额", applyRefundReq.getOrderId()),
					applySalesRefundCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_040() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 鉴定中心发货，确认收货后，全部退货退款，申请退款金额=商品可退鉴别费金额+商品可退金额+运费，申请退货退款成功");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			Thread.sleep(3000);
			
			// 发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, "Y013", 3, true);
			
			// 鉴定中心发货
			TradingCallService.authenticateDelivery(sellerId, orderId);
			
			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Integer refundProduct = orderDetailPo.getProductCount();
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight())
					.add(order.getUserAuthenticateFee(refundProduct));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight());
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);
			expectedRefundBillPo.setRefundType(applyRefundReq.getRefundType());
			expectedRefundBillPo.setUserAuthenticateFee(applyRefundReq
					.getApplyRefundAuthenticateFee());

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_041() {
		Logger.start(true,
				"买家承担鉴定费和鉴定物流费, 接单后，鉴定中心发货，买家先退一件商品，剩下商品全退，申请金额=最大可退金额+商品可退鉴定费+运费，申请成功");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			Thread.sleep(3000);
			
			// 国际段发货
			TradingCallService.delivery(sellerId, orderId, LogisticsTypeEnum.OVERSEAS_DELIVERY, "Y013", 1, true);
			
			// 鉴定中心发货
			TradingCallService.authenticateDelivery(sellerId, orderId);
			Thread.sleep(3000);

			// 先退一个商品
			BigDecimal refundedFreight = BigDecimal.ONE;
			Integer refundedProductCount = 1;

			RefundService.buildSingleApplyRefundReq(orderId, orderDetailPo,
					refundedFreight, refundedProductCount);

			Integer refundProduct = orderDetailPo.getProductCount()
					- refundedProductCount;
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight().subtract(refundedFreight))
					.add(order.getUserAuthenticateFee(refundProduct));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight().subtract(refundedFreight));
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);
			expectedRefundBillPo.setRefundType(applyRefundReq.getRefundType());
			expectedRefundBillPo.setUserAuthenticateFee(applyRefundReq
					.getApplyRefundAuthenticateFee());

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ApplyRefundV2_042() {
		Logger.start(true,
				"一个普通商品，一个鉴定商品, 接单后，鉴定商品全部退货退款，申请退款金额=商品可退鉴别费金额+鉴别运费+商品可退金额+运费，申请退货退款成功");
		try {

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticateWithTwoTypeProduct(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			Long userId = placeOrder.getUserId();

			Order order = placeOrder.getOrderList().get(0);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().stream()
					.filter(od -> Utils.falseIfNull(od.getAuthenticateFlag()) == true)
					.collect(Collectors.toList()).get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Integer refundProduct = orderDetailPo.getProductCount();
			BigDecimal totalApplyAmount = orderDetailPo
					.getProductPrice()
					.multiply(new BigDecimal(refundProduct))
					.add(order.getOrderFreight())
					.add(order.getUserAuthenticateFee(refundProduct).add(
							order.getUserAuthenticateFreightFee()));

			applyRefundReq.setOrderId(orderId);

			applyRefundReq.setApplyRefundFreight(order.getOrderFreight());
			applyRefundReq.setApplyRefundCount(refundProduct);
			applyRefundReq.setApplyRefundAmount(totalApplyAmount);
			applyRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			applyRefundReq.setApplyRefundAuthenticateFee(order
					.getUserAuthenticateFee(refundProduct)
					.add(order.getUserAuthenticateFreightFee()));
			applyRefundReq.setRefundType(1);

			applyRefundReq.setRefundBillNo(String.format("TK%s", orderId));

			applySalesRefundCall.setData(applyRefundReq);
			applySalesRefundCall.callService();

			Logger.verifyEquals("true",
					applySalesRefundCall.getString("success"), "验证success");

			String refundBillNo = applyRefundReq.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(applyRefundReq.getCatalogId());
			expectedRefundBillPo.setTotalAmount(applyRefundReq
					.getApplyRefundAmount());
			expectedRefundBillPo.setProductCount(applyRefundReq
					.getApplyRefundCount());

			expectedRefundBillPo.setFreight(applyRefundReq
					.getApplyRefundFreight());

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);
			expectedRefundBillPo.setRefundType(applyRefundReq.getRefundType());
			expectedRefundBillPo.setUserAuthenticateFee(applyRefundReq
					.getApplyRefundAuthenticateFee());

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
		
		ApplyRefundV2Req applyRefundReq = new ApplyRefundV2Req();
		ApplyRefundV2Call applySalesRefundCall = new ApplyRefundV2Call();
		
		applyRefundReq.setOrderId(1120015041);

		applyRefundReq.setApplyRefundFreight(BigDecimal.ZERO);
		applyRefundReq.setApplyRefundCount(1);
		applyRefundReq.setApplyRefundAmount(BigDecimal.ONE);
		applyRefundReq.setCatalogId("a6b8d647-cfb7-4ed0-bc53-b06e40b3299c");

		applyRefundReq.setRefundBillNo(String.format("TK%s", 1120015041));

		applySalesRefundCall.setData(applyRefundReq);
		applySalesRefundCall.callService();
	}
}
